﻿using SupermaketManager.DataAccess;
using SupermaketManager.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SupermaketManager.View
{
    public partial class FormThongKe : Form
    {
        ThongKeAccess thongkeAccess = new ThongKeAccess();
        DataTable dsThongke = new DataTable();
        public FormThongKe()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát không? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                this.Close();
            }
        }

        private void btnDetail_Click(object sender, EventArgs e)
        {
            FormDetailThongKe frmCTTK = new FormDetailThongKe();
            frmCTTK.Show();
        }

        private void FormThongKe_Load(object sender, EventArgs e)
        {
            dsThongke = thongkeAccess.layToanboDSTK();
            dtThongke.DataSource = dsThongke;
        }

        private void dtThongke_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMakiemke.Text = dtThongke.CurrentRow.Cells[0].Value.ToString();
            cbbManhanvien.Text = dtThongke.CurrentRow.Cells[1].Value.ToString();
            dtNgaykiemke.Text = dtThongke.CurrentRow.Cells[2].Value.ToString();

        }

        private void chbMahoadon_CheckedChanged(object sender, EventArgs e)
        {
            if (chbMakiemke.Checked == true)
            {
                txtMakiemke.ReadOnly = false;
                btnThem.Enabled = true;
            }
            else
            {
                txtMakiemke.ReadOnly = true;
                btnThem.Enabled = false;
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            ThongKe kk = new ThongKe(txtMakiemke.Text, cbbManhanvien.Text, DateTime.Parse(dtNgaykiemke.Text));
            if (thongkeAccess.themKiemke(kk) == 0) { MessageBox.Show("Mã kiểm kê đã tồn tại !"); }
            else
            {
                MessageBox.Show("Bạn đã thêm thành công !");

            }
            dtThongke.DataSource = thongkeAccess.layToanboDSTK();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            ThongKe kk = new ThongKe(txtMakiemke.Text, cbbManhanvien.Text, DateTime.Parse(dtNgaykiemke.Text));
            if (thongkeAccess.suaKK(kk) == 0) { MessageBox.Show("Mã kiểm kê đã tồn tại !"); }
            else
            {
                MessageBox.Show("Bạn đã sửa thành công !");

            }
            FormThongKe_Load(sender, e);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            ThongKe kk = new ThongKe(txtMakiemke.Text, cbbManhanvien.Text, DateTime.Parse(dtNgaykiemke.Text));
            if (thongkeAccess.xoaKK(kk) == 0) { MessageBox.Show("Mã kiểm kê đã tồn tại !"); }
            else
            {
                MessageBox.Show("Bạn đã xóa thành công !");

            }
            FormThongKe_Load(sender, e);
        }
    }
}
